package student.aistar.mapper;

import org.apache.ibatis.annotations.*;
import student.aistar.model.entity.*;

import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/9/27 8:32
 */
public interface OrderMapper {
    /*
     *功能描述:根据用户名查询用户
     * @date 2021/9/27
     * @param username
     * @return student.aistar.model.entity.User
     */
    @Select("select * from user where user_name = #{username}")
    User findUserByName(String username);

    /*
     *功能描述:根据用户id查询用户的所有订单
     * @date 2021/9/27
     * @param userid
     * @return java.util.List<student.aistar.model.entity.Orders>
     */
    @Select("select * from orders where user_id = #{userId}")
    List<Orders> findOrderByUser(Integer userId);

    /*
     *功能描述:根据订单id查询订单详情
     * @date 2021/9/27
     * @param orderId
     * @return java.util.List<student.aistar.model.entity.Orderdetail>
     */
    @Select("select * from orderdetail where order_id = #{orderId}")
    List<Orderdetail> findOrderDetail(Integer orderId);

    /*
     *功能描述:根据订单id查询订单
     * @date 2021/10/1
     * @param orderId
     * @return student.aistar.model.entity.Orders
     */
    @Select("select * from orders where order_id = #{orderId}")
    Orders findOrdersByOrderId(Integer orderId);

    /*
     *功能描述:根据地址id查询地址
     * @date 2021/10/1
     * @param addressId
     * @return student.aistar.model.entity.Address
     */
    @Select("select * from address where address_id = #{addressId}")
    Address findAddressByAddressId(Integer addressId);


    /*
     *功能描述:多条件查询订单,根据用户id,订单状态,订单编号查询
     * @date 2021/10/5
     * @param orderNumber
     * @param orderStatus
     * @return java.util.List<student.aistar.model.entity.Orders>
     */
    List<Orders> findOrderByNameStatus(@Param("orderNumber") Long orderNumber, @Param("orderStatus") Integer orderStatus,@Param("userId") Integer userId);

    /*
     *功能描述:根据商品名称查询订单详情
     * @date 2021/10/4
     * @param productSpecName
     * @return student.aistar.model.entity.Orders
     */
//    @Select("select * from orderdetail where product_spec_name like concat('%',#{productSpecName},'%')")
//    List<Orderdetail> findOrderDetailByOrderName(String productSpecName);

    /*查询所有订单状态
     *功能描述:
     * @date 2021/10/5
     * @param
     * @return java.util.List<student.aistar.model.entity.OrderStatus>
     */
    @Select("select * from orderstatus limit 3")
    List<OrderStatus> findAllOrderStatus();

    /*
     *功能描述:删除订单,将订单状态修改为3
     * @date 2021/10/5
     * @param orderId
     * @return int
     */
    @Update("update orders set order_status = 3 where order_id = #{orderId}")
    int delOrderlById(Integer orderId);


    /**
     * 确认购买的时候产生订单信息
     * @param order
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "orderId", keyColumn = "order_id")
    @Insert("insert into orders values(null,#{orderNumber},#{userId}, 1, 1, 0, 0, 2, '个人', 1,now(), #{orderAmount}, #{payAmount}, #{addressId}, #{buyNumber})")
    int insertOrdersComeCart(Orders order);

    /**
     * 确认购买的时候产生订单详情信息
     * @param orderDetail
     */
    @Insert("insert into orderdetail values(null, #{orderId}, #{productId}, #{productSpecName}, #{picImg}, 1, #{productSpecName}, #{price}, #{buyNumber}, #{productAmount}, 0)")
    void insertOrderDetailFormCart(Orderdetail orderDetail);


    /**
     * 更新订单状态
     * @param orderNumber
     */
    @Update("update orders set order_status = 2 where order_number = #{orderNumber}")
    void updateOrdersStatus(Long orderNumber);

    /**
     * 更新订单状态
     * @param orderNumber
     */
    @Update("update orders set order_status = 3 where order_number = #{orderNumber}")
    void updateOrdersStatusDead(Long orderNumber);


    @Select("select order_status from orders where order_number = #{orderNumber}")
    int selectOrderStatus(Long orderNumber);
}
